package com.example.basic.bytes;

import com.sun.istack.internal.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class Test {

    // 11111111111111111111111111111111
    //           1000000000000000010000

    public static void main(String[] args) {
        StateSpec stateSpec = new StateSpec(Integer.parseInt("00200010",16));

        switch (stateSpec.getAlarm()) {
            case StateSpec.ALARM_1:
                System.out.println("StateSpec.ALARM_1");break;
            case StateSpec.ALARM_2:
                System.out.println("StateSpec.ALARM_2");break;
            case StateSpec.ALARM_3:
                System.out.println("StateSpec.ALARM_3");break;
            case StateSpec.ALARM_4:
                System.out.println("StateSpec.ALARM_4");break;
            default:
                System.out.println("unknown alarm");
        }

        switch (stateSpec.getState()) {
            case StateSpec.STATE_1:
                System.out.println("StateSpec.STATE_1");break;
            case StateSpec.STATE_2:
                System.out.println("StateSpec.STATE_2");break;
            case StateSpec.STATE_3:
                System.out.println("StateSpec.STATE_3");break;
            default:
                System.out.println("unknown state");
        }
    }

    static class StateSpec {
        private static final int  STATE_1 = 1 << 0;
        private static final int  STATE_2 = 1 << 3;
        private static final int  STATE_3 = 1 << 4;

        private static final int  ALARM_1 = 1 << 16;
        private static final int  ALARM_2 = 1 << 17;
        private static final int  ALARM_3 = 1 << 21;
        private static final int  ALARM_4 = 1 << 22;

        private static final int STATE_MASK  = 0xffffffff >>> 16;
        private static final int ALARM_MASK  = 0xffffffff << 16;

        private int mState;
        public StateSpec(int state) {
            mState = state;
        }

        public int getState() {
            return mState & STATE_MASK;
        }

        public int getAlarm() {
            return mState & ALARM_MASK;
        }
    }


//    public static void main(String[] args) {
//        try {
//            File source = new File("./file.png");
//            File dst = new File("./file1.png");
//
//            buildSource(source);
//            buildDst(source, dst);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    private static void buildDst(File source, File dst) throws IOException {
        if (!dst.exists()) {
            dst.createNewFile();
        }

        FileInputStream fin = new FileInputStream(source);
        FileOutputStream fout = new FileOutputStream(dst);
        byte[] content = new byte[2];
        int i = fin.read(content);
        while (i != -1) {
            fout.write(transCode(content));
            i = fin.read(content);
        }
        fin.close();
        fout.flush();
        fout.close();
    }

    private static void buildSource(File file) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }

        byte[] pngs = new byte[1024];
        for (int i = 0; i < pngs.length; i++) {
            pngs[i] = (byte) (new Random()).nextInt(127);
        }

        pngs[10] = 0;
        pngs[11] = 125;

        pngs[40] = 0;
        pngs[41] = 91;

        pngs[100] = 0;
        pngs[101] = 93;

        pngs[400] = 0;
        pngs[401] = 44;

        pngs[1000] = 0;
        pngs[1001] = 42;

        FileOutputStream fout = new FileOutputStream(file);
        fout.write(pngs);
        fout.flush();
        fout.close();
    }

    @Nullable
    public static byte[] transCode(byte[] content) {
        if (content.length > 2) {
            throw new IllegalArgumentException("");
        }

        byte a = content[0];
        byte b = content[1];

        //0X7D
        if (a == 0 && b == 125) {
            System.out.println("满足条件1------------");
            return new byte[]{0, 125, 0, 1};
        }

        //0X5B
        if (a == 0 && b == 91) {
            System.out.println("满足条件2------------");
            return new byte[]{0, 125, 0, 2};
        }

        //0X5D
        if (a == 0 && b == 93) {
            System.out.println("满足条件3------------");
            return new byte[]{0, 125, 0, 3};
        }

        //0X2C
        if (a == 0 && b == 44) {
            System.out.println("满足条件4------------");
            return new byte[]{0, 125, 0, 4};
        }

        //0X2C
        if (a == 0 && b == 42) {
            System.out.println("满足条件5------------");
            return new byte[]{0, 125, 0, 5};
        }

        return content;
    }

}
